/**
 * create by ck on 2017/7/2.
 */
$.fn.pagination.defaults.displayMsg = '';
$(document).ready(function() {
    var groupId     = xhTool.getQuery('groupId'),
        schoolId    = xhTool.getQuery('schoolId'),
        $dataGridIn = new xhDataGrid($('#InList'), 'Appgroup/getAppGroupApp', {groupId: groupId}),
        $dataGridOut= new xhDataGrid($('#OutList'), 'Appserver/getApplication/true', {platform: 'android', schoolId: schoolId});
    $dataGridIn.init().activeButton(['bt_out'], [], 'bs');
    $dataGridOut.parseToolbar().init().activeButton(['bt_in'], [], 'bs');
    $dataGridOut.onLoadSuccess = function(data) {
        $dataGridOut.clear();
        if (data.rows.length <= 0) return false;
        $dataGridOut.mergeCells(['name'])
    };
    xhTool.bindDocKeyDown($('#bt_list_not_search').click(function () {
        $dataGridOut.reload({
            keywords: xhExtra.getValue($('#list_not_vague'))
        });
    }));
    // xhExtra.clearLoadSuccess($dataGridOut);
    //点击添加按钮，去重后提交信息到后台添加，成功后刷新页面
    $('#bt_in').click(function(){
        var rows    = $dataGridOut.list.datagrid('getSelections'),
            appList = [],
            // inRows  = $dataGridIn.list.datagrid('getRows'),
            $this   = $(this);
        $.each(rows, function(index, row){
            // var rowIndex = $dataGridIn.list.datagrid('getRowIndex', row.appId);
            // if(rowIndex >= 0 && inRows[rowIndex].versionCode === row.versionCode){
            //     return true;
            // }
            appList.push({packageName: row.packageName, versionCode: row.versionCode});
        });
        if(appList.length === 0){
            return false;
        }
        $this.append(' <i class="fa fa-spinner fa-spin fa-lg"></i>').attr('disabled', true);
        window.top.swal({
            title: '更新至应用组？',
            type: 'question',
            showCloseButton: true,
            showCancelButton: true,
            confirmButtonText: '单个更新',
            cancelButtonText: '全部更新',
            cancelButtonColor: '#e36159',
            showLoaderOnConfirm: true,
            allowOutsideClick: false
        }).then(function (isConfirm) {
            createAppGroupApp(appList, $this, 'single');
        }, function (dismiss) {
            if (dismiss === 'cancel'){
                createAppGroupApp(appList, $this, 'all');
            }
            else{
                $this.html('<i class="fa fa-long-arrow-left fa-lg"></i>').attr('disabled', false);
            }
        });
    });
    function createAppGroupApp(appList, btn, type) {
        $.ajax({
            url: baseUrl + 'Appgroup/createAppGroupApp',
            data: {groupId: groupId, appList: appList, type: type, schoolId: schoolId},
            type: 'POST',
            dataType: 'json',
            complete: function(XMLHttpRequest)
            {
                btn.html('<i class="fa fa-long-arrow-left fa-lg"></i>').attr('disabled', false);
                var result = XMLHttpRequest.responseJSON || XMLHttpRequest.responseText;
                if(xhExtra.checkError(result)) {
                    return false;
                }
                $dataGridIn.reload();
                $dataGridOut.clear();
            }
        });
    }
    //点击移除按钮，从关联变中去除记录
    $('#bt_out').click(function(){
        var packageName = $dataGridIn.getSelectValue('packageName'),
            $this       = $(this);
        $this.append(' <i class="fa fa-spinner fa-spin fa-lg"></i>').attr('disabled', true);
        $.ajax({
            url: baseUrl + 'Appgroup/delAppGroupApp',
            data: {groupId: groupId, packageName: packageName},
            type: 'POST',
            dataType: 'json',
            complete: function(XMLHttpRequest)
            {
                $this.html('<i class="fa fa-long-arrow-right fa-lg"></i>').attr('disabled', false);
                var result = XMLHttpRequest.responseJSON || XMLHttpRequest.responseText;
                if(xhExtra.checkError(result)) {
                    return false;
                }
                $dataGridIn.reload();
            }
        });
        $dataGridIn.clear();
    });
});